<template>
    <view class="container">
		<view class="cu-card">
			<view class="cu-item  shadow-warp" style="background-color: #8cabfe;" v-for="(item, index) in orders" :key="index">
				<view class="text-sm padding border-b-line" >
					<text>{{ item.orderid }}</text>
					<text class="cu-right text-xs cu-btn bg-red text-white round sm" v-if="item.status == 0">待发货</text>
					<text class="cu-right text-xs cu-btn bg-red text-white round sm" v-if="item.status == 1">在途中</text>
					<text class="cu-right text-xs cu-btn bg-green text-white round sm" v-if="item.status == 2">已签收</text>
					<text class="cu-right text-xs cu-btn bg-grey text-white round sm" v-if="item.status == 3">已取消</text>
				</view>
				<view class="cu-list">
					<view class="cu-item">
						<view class="cu-list menu-avatar">
							<view class="cu-item">
								<view class="cu-avatar lg" :style="'background-image: url('+weburl+item.goods_photo+');'"></view>
								<view class="content">
									<view class="text-cut">{{ item.goods_title }}</view>
									<view class="text-gray text-sm flex">
										<view class="text-red">{{ item.goods_price }} {{ item.buy_type == 1 ?'积分':'元' }}</view>
									</view>
									<view @click="doqianshou(item)" class="cu-right text-xl cu-btn bg-red text-white round sm" v-if="item.status == 1">确认收货</view>
									<!-- <view @click="orderCancel(item)" class="cu-right text-xl cu-btn bg-red text-white round sm" v-if="item.status == 0">取消订单</view> -->
								</view>
							</view>
						</view>
					</view>
					<view class="cu-item border-b-line padding-sm">
						<view class="text-sm">收货人：{{ item.name }} </view>
						<view class="text-sm">联系电话：{{ item.phone }} </view>
						<view class="text-sm">收货地址：{{ item.address }}</view>
						<view class="text-sm">物流单号：{{ item.tracking_number }}</view>
						
					</view>
					<view class="cu-item border-b-line padding-sm">
						<view class="text-sm">{{ item.create_time }} </view>
						
					</view>
				</view>
			</view>
		</view>
		<view class="uni-loadmore text-white" v-if="showLoadMore">{{loadMoreText}}</view>
    </view>
</template>

<script>
	export default {
        data() {
            return {
				modalName: null,
				orders: [],
				page: 1,
				lastPage: 0,
				loadMoreText: "加载中...",
				showLoadMore: true
            }
        },
		onUnload() {
			this.page = 1,
			this.lastPage = 0,
			this.list = [],
			this.loadMoreText = "加载中...",
			this.showLoadMore = true;
		},
		onLoad: function (option) {
			this.helper.checkLogin();
            this.getData();
		},
		onReachBottom() {
			// console.log("onReachBottom");
			if (this.page >= this.lastPage) {
				this.loadMoreText = "没有更多数据了!"
				return;
			}else{
				setTimeout(() => {
					this.getData();
				}, 300);
			}
		},
		onPullDownRefresh() {
			this.page = 1,
			this.lastPage = 0,
			this.orders = [],
			this.loadMoreText = "加载更多",
			
			setTimeout(() => {
				this.getData();
			}, 300);
		},
		methods: {
            getData: function() {
				//获取基础参数
				// uni.showLoading({
				// 	title:"数据加载中",
				// 	mask:true
				// })
				let page = this.page;
				let info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['page='+page,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"getGoodsOrders",
					timestamp:timestamp,
					data:{page:page,token:info.token},
					header:{sign:sign},
					method:'GET',
				}).then(res=>{
					let current_page = res.data.data.list.current_page;
					let last_page = res.data.data.list.last_page;
					this.page = current_page + 1;
					this.lastPage = last_page;
					this.orders = this.orders.concat(res.data.data.list.data);
					if (this.page >= this.lastPage) {
						this.loadMoreText = "没有更多数据了!"
					}
					uni.stopPullDownRefresh();
					uni.hideLoading();
				})
            },
			// 订单签收
			doqianshou: function(item) {
				
				var info = uni.getStorageSync("userinfo");
				let timestamp = this.helper.now();
				let sign = this.helper.setSign(['orderid='+item.orderid,'token='+info.token,'timestamp='+timestamp]);
				this.helper.req({
					api:"doqianshou", 
					timestamp:timestamp,
					data:{orderid:item.orderid,token:info.token},
					header:{sign:sign},
					method:'POST',
				}).then(res=>{
					uni.showModal({
						title: "提示",
						content: '签收成功',
						showCancel:false,
						confirmText: "确认",
						success:function(res){
							if(res.confirm){
								uni.navigateTo({
									url: "/pages/goods/orders"
								})
							}
						}
					})
				})
			},

			// 订单取消
			orderCancel: function(item) {
				var info = uni.getStorageSync("userinfo");
				let self = this;
				uni.showModal({
					title: "提示",
					content: '您确定要取消订单吗？',
					showCancel: true,
					confirmText: "确认",
					success: function(res) {
						if (res.confirm) {
							let timestamp = self.helper.now();
							let sign = self.helper.setSign(['orderid='+item.orderid,'token='+info.token,'timestamp='+timestamp]);
							self.helper.req({
								api: "orderCancel", 
								timestamp: timestamp,
								data: {orderid: item.orderid, token: info.token},
								header: {sign: sign},
								method: 'POST',
							}).then(res => {
								uni.showModal({
									title: "提示",
									content: '订单已取消',
									showCancel: false,
									confirmText: "确认",
									success: function(res) {
										if (res.confirm) {
											uni.navigateTo({
												url: "/pages/goods/orders"
											});
										}
									}
								});
							}).catch(error => {
								// 处理取消失败的情况，可以展示提示或者日志记录错误信息
								console.error('取消订单失败:', error);
							});
						}
					}
				});
			},
						
		},
    }
</script>

<style lang="scss">
	page{
		color: #56595d;
		background-color: #ddeafd;
	}
	.cu-avatar.lg {
	    width: 180upx;
	    height: 180upx;
	    background-color: #fff;
	}
	.cu-list.menu-avatar > .cu-item > .cu-avatar{
		left: 10upx;
	}
	.cu-list.menu-avatar > .cu-item .content{
		left: 210upx;
	}
</style>